package com.agnes.phone.other;

import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyPhoneStateListener2 extends PhoneStateListener {
    private Context mContext;

    public MyPhoneStateListener2(Context context) {
        mContext = context;
    }

    @Override
    public void onServiceStateChanged(ServiceState serviceState) {
        super.onServiceStateChanged(serviceState);
    }

    @Override
    public void onCallStateChanged(int state, String phoneNumber) {
        super.onCallStateChanged(state, phoneNumber);
        Log.e("PhoneCallState", Thread.currentThread().getName());
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // 响铃
                Log.e("PhoneCallState", "来电： " + phoneNumber);
                Log.e("PhoneCallState", Thread.currentThread().getName());

                /*if (phoneNumber.isEmpty()) return;

                Log.e("PhoneCallState", "来电： " + phoneNumber + "\n");
                String m = getContactNameFromPhoneBook(AppContext.getContext(), phoneNumber);
                Log.e("PhoneCallState", "来电： " + m);*/

                // 通知Activity
                Intent intent = new Intent();
                intent.setAction("com.agnes.phone");
                intent.putExtra("incomingPhone", phoneNumber);
                mContext.sendBroadcast(intent);

                break;
            case TelephonyManager.CALL_STATE_IDLE:
                // 挂断
               /* if (!TextUtils.isEmpty(phoneNumber)) {
                    new AlertDialog.Builder(mContext)
                            .setTitle("来电拦截信息")
                            .setMessage("来电号码：" + phoneNumber)
                            .setPositiveButton("确定", (dialog, which) -> dialog.dismiss())
                            .setCancelable(false)
                            .create()
                            .show();
                }*/
                Log.e("PhoneCallState", "电话挂断:" + phoneNumber);
                break;

            case TelephonyManager.CALL_STATE_OFFHOOK:
                // 接听
                break;
        }


    }


   /* @SuppressLint("CheckResult")
    private String getContactNameFromPhoneBook(Context context, String incomingNumber) {
        RxPermissions rxPermissions = new RxPermissions((MainActivity) mContext);
        final String[] contactName = {""};
        rxPermissions
                .request(Manifest.permission.CAMERA)
                .subscribe(granted -> {
                    if (granted) { // Always true pre-M
                        String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME,
                                ContactsContract.PhoneLookup.NUMBER};
                        Uri uri = Uri.withAppendedPath(
                                ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                                Uri.encode(incomingNumber));
                        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
                        if (cursor.moveToFirst()) {
                            contactName[0] = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                            cursor.close();
                        }

                    }
                });
        return contactName[0];
    }*/
}


